<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>预约看房</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f4f9;
            margin: 20px;
            color: #333;
        }
        #bookingFormContainer {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            z-index: 1000;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
        }
        .form-group button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .form-group button:hover {
            background-color: #45a049;
        }
        #closeButton {
            background-color: #f44336;
            margin-right: 10px;
        }
        #closeButton:hover {
            background-color: #d32f2f;
        }
    </style>
</head>
<body>

<button id="booking-button" class="ab">预约看房</button>

<div id="bookingFormContainer">
    <div class="form-group">
        <label for="bookingTime">预约时间:</label>
        <input type="datetime-local" id="bookingTime">
    </div>
    <div class="form-group">
        <label for="remarkInfo">备注信息:</label>
        <textarea id="remarkInfo" rows="4"></textarea>
    </div>
    <div class="form-group">
        <button id="submitButton">提交预约</button>
        <button id="closeButton">关闭</button>
    </div>
</div>

<script>
    document.getElementById('booking-button').addEventListener('click', function() {
        document.getElementById('bookingFormContainer').style.display = 'block';
    });

    document.getElementById('closeButton').addEventListener('click', function() {
        document.getElementById('bookingFormContainer').style.display = 'none';
    });

    document.getElementById('submitButton').addEventListener('click', function() {
        const bookingTime = document.getElementById('bookingTime').value;
        const remarkInfo = document.getElementById('remarkInfo').value;
        const token = localStorage.getItem('token');
        const userId = localStorage.getItem('userId');

        if (!bookingTime || !remarkInfo) {
            alert('请填写所有字段');
            return;
        }

        fetch(`http://localhost:8080/user/bookProperty`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${token}`
            },
            body: JSON.stringify({
                userId: userId,
                bookingTime: bookingTime,
                remarkInfo: remarkInfo
            })
        })
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    alert('预约成功');
                    document.getElementById('bookingFormContainer').style.display = 'none';
                } else {
                    alert('预约失败：' + data.msg);
                    document.getElementById('bookingFormContainer').style.display = 'none';

                }
            })
            .catch(error => {
                console.error('Error booking property:', error);
                alert('预约失败，请稍后再试');
                document.getElementById('bookingFormContainer').style.display = 'none';

            });
    });
</script>

</body>
</html>
